$("#uploadWork .headerBox").load("header.html");
$("#uploadWork .footer").load("footer.html");


//用于截取字符信息的方法

function getSearchObj(){
    var  qs = location.search.length>0 ? location.search.substr(1):'',
    args = {},  
    items = qs.length>0 ? qs.split('&'):[],
    item = null,name = null,value = null,i = 0,len = items.length;

    for(i = 0;i < len; i++){
        item = items[i].split('=');
        name = decodeURIComponent(item[0]);
        value = decodeURIComponent(item[1]);

        if(name.length){
            args[name] = value;
        }
    }

    return args;
}
//用于获取信息,并在信息栏中展示信息

var information = getSearchObj()
$(".contentBox .thirdP .con").html(information.coursesChnName);
$(".contentBox .fourP .con").html(information.classInfor);
$(".contentBox .sixP .con").html(information.workName);


// 文件上传
$("#uploadWork .firstP").click(function(){
	$("#login .webuploader-pick").html("继续上传")
})

var uploader = WebUploader.create({
    // swf文件路径
    swf: "../../config/Uploader.swf",

    // 文件接收服务端。
    server: 'http://192.168.4.102:8090/oss/file/upload',

    // 选择文件的按钮。可选。
    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
    pick: '.firstP',
    
     // 选择文件的类型。
    accept: {
        extensions: '*',//ppt,pdf,word,
       
    },

    // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
    resize: false
});
var $list = $("#uploadWork .workLists");
//当有文件添加进来时

state = 'pending';
uploader.on( 'fileQueued', function( file ) {
	var num = $list.children().length + 1;
	
    $list.append( '<div id="' + file.id + '" class="item clearfix">' +
    	"<p class='pull-left upAA'>" + num +  "</p>" +
        '<p class="info pull-left">作业标题：' + file.name + '</p>' +
        '<p class="del pull-right">删除</p>' + 
        '<p class="state pull-right">等待上传</p>' +
        
    '</div>' );
    $("#uploadWork .workLists .item .del").on("click",function(){
    	var id = $(this).parent().attr("id");
    	$(this).parent().remove();
		uploader.removeFile(id);
	})
});


uploader.on('uploadProgress', function (file, percentage) {
	var $li = $('#' + file.id),
	$percent = $li.find('.progress .bar');
	
	// 避免重复创建
	if (!$percent.length) {
		$percent = $('<span class="progress">' +
		'<span class="percentage"><span class="text"></span>' +
		'<span class="bar" role="progressbar" style="width: 0%">' +
		'</span></span>' +
		'</span>').appendTo($li).find('.bar');
	}
	$li.find('p.state').text('上传中');
	$li.find("span.text").text(Math.round(percentage * 100) + '%');//显示上传进度
	$percent.css('width', percentage * 100 + '%');
});

uploader.on('uploadSuccess', function (file,response) { //response  用于从服务器返回的数据
	$('#' + file.id).find('p.state').text('已上传');
	//这里可以得到返回的数据
//	$.ajax({
//		type:"POST",
//		url:ptms.url.common.courseManger.saveWorkInformation,
//		data:{
//			"spcId":information.subId,
////			"key":key
//		},
//		success:function(data){
//			
//		}
//	})
	
});

uploader.on('uploadError', function (file) {
	
	$('#' + file.id).find('p.state').text('上传出错');
});

 

uploader.on('uploadComplete', function (file) {
	$('#' + file.id).find('.progress').fadeOut();
});

uploader.on('all', function (type) {
	
	if (type === 'startUpload') {
	state = 'uploading';
	} else if (type === 'stopUpload') {
	state = 'paused';
	} else if (type === 'uploadFinished') {
	state = 'done';
	}
	
	if (state === 'uploading') {
	$btn.text('暂停上传');
	} else {
	$btn.text('开始上传');
	}
});
$btn = $("#uploadWork .submitBox p span.firstSpan");
$btn.on('click', function () {
	alert(1);
	if (state === 'uploading') {
	uploader.stop(true);//不加ture暂停上传没用！
	} else {
	uploader.upload();
	}
});

